package prototype_pattern;

/**
 * 原型模式
 */
public class Prototype {
  public static void main(String[] args){
    Mail mail = new Mail();
    mail.setObject(new Integer("2019"));
    mail.setMsg("happy birthday");
    Mail mail1 = (Mail) mail.clone();
    mail.setName("san");
    mail1.setName("min");
    mail.setObject(new Integer("2018"));
    System.out.println(mail.getObject()==mail1.getObject());
    System.out.println(mail.toString());
    System.out.println(mail1.toString());
  }
}

class Mail implements Cloneable{
  private String name;
  private String msg;
  private Object object;

  public Object getObject() {
    return object;
  }

  public void setObject(Object object) {
    this.object = object;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  public Object clone(){
    Object clone = null;
    try {
      clone = super.clone();
    } catch (CloneNotSupportedException e) {
      e.printStackTrace();
    }
    return clone;
  }

  @Override
  public String toString() {
    return name+":"+msg;
  }
}
